import React, { Component } from 'react'
import fetch from 'node-fetch'

function checkStatus(res) {
  if (res.ok) {
    // res.status >= 200 && res.status < 300
    return res
  }
  throw new Error('fetch user error', res.statusText)
}

export default (mapUserToProps, loadingProps) => warpperComponent => class componentName extends Component {
  componentDidMount() {
    const url = encodeURI('/api/v2/users/auth0|5c29e2031f9dc0764b7343ea?params=name,picture')
    fetch(url, {
      method: 'get',
      headers: {
        authorization:
            'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik1EQXlSa1pFT0RreE5EY3hRa00zT0RSQ05ERXlRVVU0T0RVd05EaEdOVFEwTURGRk9UVTNOdyJ9.eyJodHRwczovL2hhc3VyYS5pby9qd3QvY2xhaW1zIjp7IngtaGFzdXJhLWRlZmF1bHQtcm9sZSI6InVzZXIiLCJ4LWhhc3VyYS1hbGxvd2VkLXJvbGVzIjpbInVzZXIiXSwieC1oYXN1cmEtdXNlci1pZCI6ImF1dGgwfDVjMjQzY2Q4ZTkyZTNlMzdlY2ViOTY1YyJ9LCJuaWNrbmFtZSI6IjMxMzk5OTcxNyIsIm5hbWUiOiIzMTM5OTk3MTdAcXEuY29tIiwicGljdHVyZSI6Imh0dHBzOi8vcy5ncmF2YXRhci5jb20vYXZhdGFyLzlkMWNjM2NmM2FmODg2Yjk5ZjE1NjNiMzU4MDRhNmFiP3M9NDgwJnI9cGcmZD1odHRwcyUzQSUyRiUyRmNkbi5hdXRoMC5jb20lMkZhdmF0YXJzJTJGMzEucG5nIiwidXBkYXRlZF9hdCI6IjIwMTgtMTItMzFUMDk6MzM6NTMuOTg2WiIsImVtYWlsIjoiMzEzOTk5NzE3QHFxLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwiaXNzIjoiaHR0cHM6Ly94aW5namluZy5hdS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWMyNDNjZDhlOTJlM2UzN2VjZWI5NjVjIiwiYXVkIjoiVWhGVVQ2WGtJc3RHN2xhNVN1TzREcVZDNUJLQjJ0YnIiLCJpYXQiOjE1NDYyNTI0MTQsImV4cCI6MTU0NjI4ODQxNCwiYXRfaGFzaCI6IllLR2FmZWU4c2ZNejFHb0dlN05BQnciLCJub25jZSI6ImlMZFRJcGhkQnFVSUUwUk5KTVVFQlZVd2hFUEYxM3NDIn0.Dt_tcPmpbmFYhthBUOK4q_smKkfbjHsCv6OFP9_K0ReIbeyt_WZPk7LaXaLvpO3cDevcxmN0urzeCFdXy7MBNcFyV3bDkAsLZcV57SW0gWzwYuvsivqj4kkuLp8LUFm23SxmRLz56QQm71y4MGn7XfpGZ2OoqfEHqG2cOYZBNor2rI3Id7oXT2YIiY8rM-2Mb-HsfA-7-pT9HzQkvGxxQPHIuDvFHGpCLejaVyIrR0e6op7fO75ZhuG5banmLJYNf2YfKO6Z-ZT1QNp3crBq1poKLE4SJPDaHJ7XEMex2VGoK5hmr3WSaqP_xKxb55QkZAGg1oJj1kwNzLhhfRepnQ',
      },
    })
      .then(checkStatus)
      .then(res => console.log('will not get here...'))
      .catch((error) => {
        if (error.status === 404) {
          // do something about 404
        }
      })
  }

  render() {
    return <warpperComponent />
  }
}
